
package test;

import java.util.Properties;
import javax.mail.Address;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class test {
	public static void main(String[] args) {
		
		sendMessage();
		//readMessage();
	}
	
	public static void readMessage()
	{
		Properties props = System.getProperties();
		props.setProperty("mail.store.protocol", "imaps");
		try 
                {
			Session session = Session.getDefaultInstance(props, null);
			Store store = session.getStore();

			store.connect("imap.gmail.com", "dangxuanthong@gmail.com",
					"!vinamilk100G!");
			Folder f = store.getFolder("INBOX");
                        //f.getMessages()[0].getC

                } 
                catch (MessagingException e) 
                {
			e.printStackTrace();
			System.exit(2);
		}
	}
	
	public static void sendMessage()
	{
		//send message with SSL secure

		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");
 
		Session session = Session.getDefaultInstance(props,
			new javax.mail.Authenticator() 
                        {
                                @Override
				protected PasswordAuthentication getPasswordAuthentication()
				{
					return new PasswordAuthentication("dangxuanthong","!vinamilk100G!");
				}
			});
 
		try {
 
			Message message = new MimeMessage(session);                        
			message.setFrom(new InternetAddress("khi@no-spam.com"));
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse("dangxuanthong@gmail.com"));
                        message.setRecipients(Message.RecipientType.CC,
					InternetAddress.parse("test.thunderpig@gmail.com"));
                        message.setRecipients(Message.RecipientType.BCC,
					InternetAddress.parse("customer.thunderpig@gmail.com"));
			message.setSubject("Testing Subject2");
			message.setText("Dear Mail Crawler," +
					"\n\nNo spam to my email, please!");
 
                        
			Transport.send(message, InternetAddress.parse("user1.thunderpig@gmail.com"));
 
			System.out.println("Done");
 
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}
}

    

